package com.zzj.utils;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class RSAUtil {


    public static final String PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJ2bigtQ9LgnSZLesAUU7BsKKCeaxc/ZyG2DZcmneCRNpQmkK+AWXonVvhkkYaJOrU5xXW2R2qBMNbS5rNcTSMjRrC48Cthby+UnsIdvOtTRvhs93g2v/zGyk2UsxDYzK9HQby8wdhQ0SpJOxHgTkbFgZEezF8n7CbyTUYLgU1IHAgMBAAECgYAd3bKcEFw/ZVqA1WgfnmZbfWI8lwyzJX7gmcy5GN4gawJCQHT8AuEpV3KtiwJhYm1e9waQU9pNFAP9Wk4Rd5sL/pE5oLeYFpGZSHssS2ayfXX5sOSU+FJfQ/G2jDCc2EFidROPs2Q4Nd/hNYRPf7Yg2JI8ps2gBeciz7Z8UNsN6QJBANyXO8kn24cbKzFyWTkOXyijY9gWfyt/+AiAugzigA+m9yZhoY7LSDL0KNvQEI2oO1H+rn0zv1Dwm/9aTPBX+10CQQC26CEOqK0C/rCklUcKBQHpfdgNXL1voQT4MuXgDwo+rtROR+o1QxPcudmetOTFTm+B31ZxO0gqGu0xA65qr9CzAkA+ONJ+XSXJKFq4qd6M/4bL+bqSG1ZnuMMH8veH5uPkhl40j1l449JLvR9BUaE5s/h3mnUxj8UpWZVHI/uL1+VpAkBX/Q1CBwTLxwfk9ObqvAPydhaEPxPmaniAicOp6LJN+z6otdHGc5FPgpxcy7qt7BCaUn08FX+eubtW8+Vo71EfAkEAt1OKScFvQULVpGWHjeofDZNNi/zw1FFeUGTlbARA0IdfPxtLvuMD5Yu6W4WIMZgIb+Ongyq9aHCfE169yTx97A==";
    public static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCdm4oLUPS4J0mS3rAFFOwbCignmsXP2chtg2XJp3gkTaUJpCvgFl6J1b4ZJGGiTq1OcV1tkdqgTDW0uazXE0jI0awuPArYW8vlJ7CHbzrU0b4bPd4Nr/8xspNlLMQ2MyvR0G8vMHYUNEqSTsR4E5GxYGRHsxfJ+wm8k1GC4FNSBwIDAQAB";


    public static String encrypted(String text) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, loadPublicKey(PUBLIC_KEY));
        byte[] encodedBytes = cipher.doFinal(text.getBytes());
        String ciphertext = Base64.getEncoder().encodeToString(encodedBytes);
        return ciphertext;
    }

    public static String decrypted(String ciphertext) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, loadPrivateKey(PRIVATE_KEY));
        byte[] decodedBytes = Base64.getDecoder().decode(ciphertext);
        byte[] decrypted = cipher.doFinal(decodedBytes);
        return new String(decrypted);
    }

    public static PublicKey loadPublicKey(String publicKeyStr) throws Exception {
        PublicKey publicKey;
        byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyStr);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        publicKey = keyFactory.generatePublic(keySpec);
        return publicKey;
    }

    public static PrivateKey loadPrivateKey(String privateKeyStr) throws Exception {
        PrivateKey privateKey;
        byte[] privateKeyBytes = Base64.getDecoder().decode(removePrivateKeyHeader(privateKeyStr));
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        privateKey = keyFactory.generatePrivate(keySpec);
        return privateKey;
    }
    private static String removePrivateKeyHeader(String privateKey) {
        String strippedKey = privateKey.replaceAll("-----BEGIN (.)-----", "");
        strippedKey = strippedKey.replaceAll("-----END (.)-----", "");
        strippedKey = strippedKey.replaceAll("\s+", "");
        return strippedKey;
    }
}
